import 'package:flutter/material.dart';

class Test_LayoutBuilderPage extends StatelessWidget {
  const Test_LayoutBuilderPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Test_LayoutBuilderPage')),
        body:  Column(children: [
          Test_LayoutBuilder(children: List.filled(6, Text("A")),),
          LayoutBuilderRoute()
        ])
    );
  }
}

class LayoutBuilderRoute extends StatelessWidget {
  const LayoutBuilderRoute({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var _children = List.filled(6, Text("A"));
    // Column在本示例中在水平方向的最大宽度为屏幕的宽度
    return Center(child: Column(
      children: [
        // 限制宽度为190，小于 200
        SizedBox(width: 190, child: Test_LayoutBuilder(children: _children)),
        Test_LayoutBuilder(children: _children),
        // LayoutLogPrint(child:Text("xx")) // 下面介绍
      ],
    ));
  }
}

class Test_LayoutBuilder extends StatelessWidget {
  const Test_LayoutBuilder({Key? key, required this.children}) : super(key: key);

  final List<Widget> children;

  @override
  Widget build(BuildContext context) {
    // 通过 LayoutBuilder 拿到父组件传递的约束，然后判断 maxWidth 是否小于200
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        print('JtTag:maxWidth:${constraints.maxWidth}:maxHeight:${constraints.maxHeight}');
        print('JtTag:minWidth:${constraints.minWidth}:minHeight:${constraints.minHeight}');
        if (constraints.maxWidth < 200) {
          // 最大宽度小于200，显示单列
          return Column(children: children, mainAxisSize: MainAxisSize.min);
        } else {
          // 大于200，显示双列
          var _children = <Widget>[];
          for (var i = 0; i < children.length; i += 2) {
            if (i + 1 < children.length) {
              _children.add(Row(
                children: [children[i], children[i + 1]],
                mainAxisSize: MainAxisSize.min,
              ));
            } else {
              _children.add(children[i]);
            }
          }
          return Column(children: _children, mainAxisSize: MainAxisSize.min);
        }
      },
    );
  }
}

